<?php


namespace app\admin\controller\zhougong;


use app\admin\controller\Base;
use app\common\enums\ErrorCode;
use app\common\juhe\Curl;
use app\common\vo\ResultVo;
use think\Db;
use think\facade\Request;

class ZhouGongController extends Base
{
    public function search(Request $request)
    {
        try {
            $url = "http://v.juhe.cn/dream/query";
            $params = array(
                "key" => '9d41ca68a2efe2cf6c9f64c02a749ec3',//应用APPKEY(应用详细页查询)
                "q" => Request::param('keywords', ''),//梦境关键字，如：黄金 需要utf8 urlencode
                "cid" => "",//指定分类，默认全部
                "full" => 1,//是否显示详细信息，1:是 0:否，默认0
            );
            if ($params['q']) {
                Db::name('zhougong')->insert(['keywords' => $params['q']]);
                $paramstring = http_build_query($params);
                $content = Curl::juhecurl($url, $paramstring);
                $result = json_decode($content, true);
                if ($result['result']) {
                    return ResultVo::success($result['result']);
                } else {
                    return ResultVo::error(ErrorCode::DATA_NOT);
                }
            } else {
                return ResultVo::error(ErrorCode::DATA_NOT);
            }
        } catch (\Exception $e) {
            return ResultVo::error(ErrorCode::VALIDATION_FAILED);
        }
    }

}